#include "head.h"


void do_stuff(int sockfd, MSG *pbuf);
void do_admin(int sockfd, MSG *pbuf);
void show_began(MSG *pbuf);

//用户登录
int level = 0;

void get_from_stdin(MSG *pbuf)
{
	char str[32];
	int number = 17;
	while(number > 16)
	{
		printf("请输入用户名:\n");
		number = scanf("%s",str);
	}
	strcpy(pbuf->data,str);
	
	number = 17;
	while(number > 16)
	{
		printf("请输入新的passwd:\n");
		number = scanf("%s",str);
	}
	strcpy(pbuf->passwd,str);		

	number = 100000001;
	while(number > 100000000)
	{
		printf("请输入新的id:\n");
		scanf("%d",&number);
	}		
	pbuf->info.id = number;

	number = 17;
	while(number > 16)
	{
		printf("请输入新的员工名:\n");
		number = scanf("%s",str);
	}
	strcpy(pbuf->info.name,str);

	number = 17;
	while(number > 16)
	{			
		printf("请输入新的地址:\n");
		number = scanf("%s",str);
	}
	strcpy(pbuf->info.addr,str);	

	number = 201;
	while(number > 200)
	{
		printf("请输入新的年龄:\n");
		scanf("%d",&number);
	}
	pbuf->info.age = number;

	float salary;
	salary = 100001;
	while(salary > 100000.0)
	{
		printf("请输入新的工资:\n");
		scanf("%f",&salary);
	}
	pbuf->info.salary = salary;
	
	number = -1;
	while(number !=0 && number != 1)
	{
		printf("请输入新的权限:\n");
		scanf("%d",&number);
	}
	pbuf->ret = number;

}

void do_change(int sockfd, MSG *pbuf)
{
	char ed;
	char getstr[20];
	if(level == 1)
	{
		printf("你是否要修改其他用户信息？(Y/N)\n");
		while((ed = getchar()) != '\n' && ed != EOF) ;
		ed = getchar();
		if(ed == 'Y')
		{
			get_from_stdin(pbuf);
		 	pbuf->type = ADMIN_CHANGE_INFO;
		}
	}	
	if(level == 0 || ed == 'N')
	{		
		while(scanf("%s",getstr) < 0)
		{
			printf("出错！！请重新输入。");
			while((ed = getchar()) != '\n' || ed != EOF);
		}
		strcpy(pbuf->data,getstr);
		pbuf->type = STAFF_CHANGE_PASSWD;
	}
	send(sockfd,pbuf,sizeof(MSG),0);
	recv(sockfd,pbuf,sizeof(MSG),0);
	printf("change return:%s\n",pbuf->data);
}
void do_show(int sockfd, MSG *pbuf)
{
	if(level == 0)
	{
		strcpy(pbuf->data,pbuf->name);
		pbuf->type = STAFF_QUERY;
	}
	else 
	{
		printf("请输入你要查询的用户：\n");
		scanf("%s",pbuf->data);
		pbuf->type =  ADMIN_QUERY;
	}
	send(sockfd,pbuf,sizeof(MSG),0);
	
	recv(sockfd,pbuf,sizeof(MSG),0);
	if(pbuf->ret == -1)
		printf("%s\n",pbuf->data);
	else
	{
		printf("name = %s,passwd = %s,\nid = %d,true name = %s\naddr = %s,age = %d,salary = %f,level = %d(1:admin 0:staff)\n",\
			pbuf->data,pbuf->passwd,pbuf->info.id,\
			pbuf->info.name,pbuf->info.addr,pbuf->info.age,\
			pbuf->info.salary,pbuf->ret);
	}
	return;
}

void do_delete(int sockfd, MSG *pbuf)
{
	char name[10];
	printf("请输入要删除的用户的用户名：\n");
	while(scanf("%s",name) < 0)
	{
		printf("请输入正确的用户名:\n");
	}
	stpcpy(pbuf->data,name);
	pbuf->type = ADMIN_DEL_STAFF;
	send(sockfd,pbuf,sizeof(MSG),0);
	recv(sockfd,pbuf,sizeof(MSG),0);
	printf("delete return:%s\n",pbuf->data);
}
void do_add(int sockfd, MSG *pbuf)
{

	get_from_stdin(pbuf);

	pbuf->type = ADMIN_ADD_STAFF;
	send(sockfd,pbuf,sizeof(MSG),0);
	recv(sockfd,pbuf,sizeof(MSG),0);
	printf("add return:%s\n",pbuf->data);
}

int do_login(int sockfd, MSG *pbuf)
{
	char ed;
	//保存登陆的结果
	int ret = -1; 
	//在成功登陆前无限循环
	printf("began login\n");
	while(!(ret == 1 || ret == 0) ){
		//从终端接受用户名和密码
	    printf("name:");
		fflush(stdout);
		scanf("%s",pbuf->name);
		printf("passwd:");
		fflush(stdout);
		scanf("%s",pbuf->passwd);
		//设置信息类型为 LOGIN
		pbuf->type = LOGIN;
		//发送信息
		send(sockfd,pbuf,sizeof(MSG),0);
		//接受信息
		recv(sockfd,pbuf,sizeof(MSG),0);
		ret = pbuf->ret;
		printf("the login return:%s\n",pbuf->data);
		while((ed = getchar()) != '\n' && ed != EOF);
	}
	level = pbuf->ret;
	if(level == 1)
		do_admin(sockfd,pbuf);
	else if(level == 0)
		do_stuff(sockfd,pbuf);
		
	return 0;
}

void do_stuff(int sockfd, MSG *pbuf)
{
	char s;
	show_began(pbuf);
	while(1)
	{
		printf("bagan use:\n");
		s = getchar();
		switch(s){
			case 'Q': send(sockfd,NULL,0,0);return;break;
			case 'S': do_show(sockfd,pbuf);break;
			case 'C': do_change(sockfd,pbuf);break;
			default : printf("请按要求输入！！！");break;
		}
		while((s = getchar()) != '\n' && s != EOF);
		
	}
}

void do_admin(int sockfd, MSG *pbuf)
{ 
	char s;
	show_began(pbuf);
	while(1)
	{
		printf("bagan use:\n");
		s = getchar();
		switch(s){
			case 'Q': return;break;
			case 'S': do_show(sockfd,pbuf);break;
			case 'C': do_change(sockfd,pbuf);break;
			case 'D': do_delete(sockfd,pbuf);break;
			case 'A': do_add(sockfd,pbuf);break;
			default : printf("请按要求输入！！！");break;
		}
		while((s = getchar()) != '\n' && s != EOF);
		
	}

    
}
void show_began(MSG *pbuf)
{
	char level[16] = {'\0'};
	if(pbuf->ret == 1)
		strcpy(level,"管理员");
	else
		strcpy(level,"员工");
	printf("********欢迎使用员工管理系统********\n");
	printf("***                              ***\n");
	printf("user:%10s       登陆模式:%4s     \n",pbuf->name,level);      
    	printf("***                              ***\n");
	printf("***  Q  Quit                     ***\n");
	printf("***  S  Show information         ***\n");
	if(pbuf->ret == 0)
	{
		printf("***  C  Change passwd            ***\n");
	}else if(pbuf->ret == 1)
	{
		printf("***  C  Change information       ***\n");
		printf("***  D  Delete staff             ***\n");
		printf("***  A  Add staff                ***\n");
	}
	printf("************************************\n");
	return;
}

int main(int argc, char *argv[])
{
    int sockfd, ret;
    struct sockaddr_in servaddr;
    MSG buf;
    bzero(&buf,sizeof(MSG));
    int longth_addr = sizeof(struct sockaddr);
    char clean[64];
	
    if (argc < 3)
    {
        printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
        exit(-1);
    }

    //创建客户端socket
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("fail to socket");
        exit(-1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(atoi(argv[2]));
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);
	
    //连接服务器
	if((ret = connect(sockfd,(struct sockaddr *)&servaddr,longth_addr)) != 0)
	{
	    printf("connect server error!!\n");
		return -1;
	}
	do_login(sockfd,&buf);
	
	close(sockfd);
	return 0;
}
